package com.hydl.power.service.impl;

import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.HomeService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HomeServiceImpl  implements HomeService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private IncomingCabinetMapper incomingCabinetMapper;


    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private PowerRoomMapper powerRoomMapper;

    @Autowired
    private TransformerMapper transformerMapper;

    @Autowired
    private TemperatureDeviceMapper temperatureDeviceMapper;

    @Autowired
    private CameraMapper cameraMapper;

    @Autowired
    private LoadMapper loadMapper;
    @Autowired
    private IdenticalMapper identicalMapper;



    @Override
    public HomeData getPowerData(String roomId) throws ParseException {
       /* Project project=projectMapper.getProjectByLongitudeAndLatitude(longitudeLatitude,latitudeLatitude);
        if (project==null){
            return null;
        }*/

        HomeData homeData=new HomeData();
        //带能分析
        String dayPowerData = incomingCabinetMapper.getDayPowerDataByRoomId(roomId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Date> dateList = dateToWeek(simpleDateFormat.format(new Date()));
        String weekPowerData = incomingCabinetMapper.getWeekPowerDataByRoomId(roomId,dateList.get(0),dateList.get(6));
        String monthPowerData  = incomingCabinetMapper.getMonthPowerDataByRoomId(roomId);
        String sumPowerData = incomingCabinetMapper.getSumPowerDataByRoomId(roomId);
        homeData.setDayPowerData(dayPowerData==null?"0":dayPowerData);
        homeData.setWeekPowerData(weekPowerData==null?"0":weekPowerData);
        homeData.setMonthPowerData(monthPowerData==null?"0":monthPowerData);
        homeData.setSumPowerData(sumPowerData==null?"0":sumPowerData);
        //告警分析
        String generalUntreated =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,1,1);
        String generalProcessing =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,1,3);
        String generalProcessed =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,1,5);
        String urgentUntreated =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,2,1);
        String urgentProcessing =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,2,3);
        String urgentProcessed =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,2,5);
        String seriousUntreated =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,3,1);
        String seriousProcessing =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,3,3);
        String seriousProcessed =orderDeliveryMapper.getAlarmTotalByRoomId(roomId,3,5);
        homeData.setGeneralUntreated(generalUntreated);
        homeData.setGeneralProcessing(generalProcessing);
        homeData.setGeneralProcessed(generalProcessed);
        homeData.setUrgentUntreated(urgentUntreated);
        homeData.setUrgentProcessing(urgentProcessing);
        homeData.setUrgentProcessed(urgentProcessed);
        homeData.setSeriousUntreated(seriousUntreated);
        homeData.setSeriousProcessing(seriousProcessing);
        homeData.setSeriousProcessed(seriousProcessed);
        return homeData;
    }

    @Override
    public List<String> getPowerRoomData(String roomId) {
        /*Project project=projectMapper.getProjectByLongitudeAndLatitude(longitudeLatitude,latitudeLatitude);
        if (project==null){
            return null;
        }*/
        List<String> list=new ArrayList<>();
        String deviceNumber = deviceMapper.getDeviceNumberByRoomId(roomId);
        String transformerNumber = transformerMapper.getTransformerMapperNumberByRoomId(roomId);
        String temperatureDeviceNumber = temperatureDeviceMapper.getTemperatureDeviceNumberByRoomId(roomId);
        String cameraDeviceNumber = cameraMapper.getCameraDeviceNumberByRoomId(roomId);
        /*String powerRoomNumber = powerRoomMapper.getPowerRoomNumberByProjectId(project.getProjectId());*/
        list.add(deviceNumber);
        list.add(transformerNumber);
        list.add(cameraDeviceNumber);
        list.add(temperatureDeviceNumber);

        return list;
    }

    @Override
    public List<List<String>> getLoadData(String roomId) {
      List<Transformer> transformer=transformerMapper.selectByCondition(null,null,null,null,roomId);
      System.out.println(transformer);
      DecimalFormat df = new DecimalFormat("0.00%");
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

      List<Load> list= loadMapper.queryloadRate(roomId);
      for (Load load : list) {
        for (Transformer transformer1 : transformer) {
          if(load.getDeviceName().equals(transformer1.getDeviceName())){
            load.setLoadRate(df.format(Double.parseDouble(load.getPtin())*1.732/Double.parseDouble(transformer1.getTransformerCapacity())));
            load.setCreateTime(load.getCreateTime().substring(11));
          }
        }
      }
      System.out.println(list);
      List deviceNameAll=new ArrayList();
      List data=new ArrayList();
      List time=new ArrayList();
      List lists=new ArrayList();
      Map map = new HashMap();
/*      for (Load load : list) {
        deviceNameAll.add(load.getDeviceName());
      }
      HashSet<String> set=new LinkedHashSet<>(deviceNameAll);
      System.out.println(set);*/
      for (Transformer s : transformer) {
        map.put("transformerName",s.getTransformerName());
        map.put("deviceName",s.getDeviceName());
        for (Load load : list) {
          if(s.getDeviceName().equals(load.getDeviceName())){
            data.add(load.getLoadRate());
            time.add(load.getCreateTime());
          }
        }
        map.put("data",data);
        map.put("time",time);
        lists.add(map);
        //清除集合中数据，让下一次循环继续使用
        data=new ArrayList();
        time=new ArrayList();
        map = new HashMap();
      }

        return lists;
    }

    @Override
    public List<PowerRoom> getPowerRoom(String roomName) {
        List<PowerRoom> list= powerRoomMapper.getPowerRoom(roomName);
        return list;
    }

    /**
     * 根据日期获得所在周的日期
     * @param mdate
     * @return
     */
    public static List<Date> dateToWeek(String mdate) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = simpleDateFormat.parse(mdate);
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(parse);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
        Date mondayDate = cal.getTime();
        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        List<Date> lDate = new ArrayList<>();
        lDate.add(mondayDate);
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(mondayDate);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(sundayDate);
        while (sundayDate.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

  @Override
  public List<Identical> queryYearOnYearDays(String roomId) {
    DecimalFormat df = new DecimalFormat("0.00%");
      List<Identical> list = identicalMapper.queryYearOnYearDays(roomId);
      if(list!=null){
        for (Identical identical : list) {
          if(identical.getUpValue() == null){
            identical.setUpValue(0);
          }
          if(identical.getCurValue()>identical.getUpValue()){
            double percent=(identical.getCurValue()-identical.getUpValue());
            percent=percent/identical.getUpValue();
            identical.setPercent(df.format(percent));
          }
          if(identical.getUpValue()==0 && identical.getCurValue()==0){
            identical.setPercent("0%");
          }else {
            double percent=(Double.parseDouble(identical.getCurValue().toString())/Double.parseDouble(identical.getUpValue().toString())-1);
            identical.setPercent(df.format(percent));
          }
        }
        return list;
      }
    return null;
  }
}

